package epam.car.rent.dialogs;

import java.io.FileNotFoundException;
import java.util.*;
import epam.car.rent.*;
import epam.car.rent.exceptions.*;
import dataAccess.*;


@Deprecated
public class Dialogs {
	public static void helloDialog(){
		System.out.println("Hello there. It's a prototype of a car rent system." +
							"\nBelow are available actions, feel free to choose any:");
		availableCommands();
	}
	
	public static void availableCommands(){
		System.out.println("1. Data Base import\n2. Data Base export\n3. Print records" +
				"\n4. Create objects\n5. Print all errors\n6. Import all from DB\n7. Export all to DB\nEnter the number of the command: ");
		String command;
		try {
			command = new Scanner(System.in).nextLine();
			switch(Integer.valueOf(command)){
			case 1:
				importBaseDialog();
				break;
			case 2:
				exportBaseDialog();
				break;
			case 3:
				printRecordsDialog();
				break;
			case 4:
				createObjectsDialog();
				break;
			case 5:
//				ExceptionLogger.getLogger().printErrors();
				availableCommands();
				break;
			case 6:
//				Management.requestFromDb();
				DbWriter.requestAllFromDb();
				availableCommands();
				break;
			case 7:
//				Management.storeToDb();
				DbWriter.storeAllToDb();
				availableCommands();
				break;
			default:
				System.out.println("There's no such option. \n");
				helloDialog();
				break;
			}
		} catch (NumberFormatException e) {
			System.out.println("The number you've entered is incorrect.\n");
			helloDialog();
		}catch (NoSuchElementException e1) {
			System.out.println("There's no such element!");
		}
	}
	
	public static void importBaseDialog() {
		System.out.println("Please, specify a path to folder where the base is placed or 0 to go back to main menu: ");
		String pathToBase = new Scanner(System.in).nextLine();
		if(pathToBase.equals("0")){
			availableCommands();
		}
		if(!pathToBase.endsWith("\\")){
			pathToBase += "\\";
		}
		System.out.println("Please, specify a file format for base files (TXT, CSV, JSON): ");
		String fileFormat = new Scanner(System.in).nextLine();
		fileFormat = fileFormat.toUpperCase();
		try {
			Management.importBaseFromFile(pathToBase, WriterType.valueOf(fileFormat));
			System.out.println("\nBase is loaded.\n");
			availableCommands();
		} catch (FileNotFoundException e) {
			System.out.println("Can't find a base by specified path! Please check!");
			availableCommands();
		} catch (IllegalArgumentException e) {
			System.out.println("You've selected a format that is not listed!");
			importBaseDialog();
		}
	}
	
	static void exportBaseDialog(){
		System.out.println("Please, specify a path to folder in which you want to export the base or 0 to go back to main menu: ");
		String pathToBase = new Scanner(System.in).nextLine();
		if(pathToBase.equals("0")){
			availableCommands();
		}
		if(!pathToBase.endsWith("\\")){
			pathToBase += "\\";
		}
		System.out.println("Please, specify a file format for base files (TXT, CSV, JSON): ");
		String fileFormat = new Scanner(System.in).nextLine();
		fileFormat = fileFormat.toUpperCase();
		try {
			Management.exportBaseToFile(pathToBase, WriterType.valueOf(fileFormat));
			System.out.println("Base exported.\n");
			availableCommands();
		} catch (FileNotFoundException e) {
			System.out.println("Can't export a base to specified path! Please check!");
			importBaseDialog();
		} catch (IllegalArgumentException e) {
			System.out.println("You've selected a format that is not listed!");
			importBaseDialog();
		}
	}
	
	static void printRecordsDialog() {
		System.out.println("What list you'd like to see?\n");
		System.out.println("1. Clients\n2. Cars\n3. Orders\n4. Whole list\nEnter the number of the command or 0 to go back to main menu: ");
		String command = new Scanner(System.in).nextLine();
		try {
			switch(Integer.valueOf(command)){
				case 1:
					Management.printClients();
					System.out.println("\n");
					availableCommands();
					break;
				case 2:
					Management.printCars();
					System.out.println("\n");
					availableCommands();
					break;
				case 3:
					Management.printOrders();
					System.out.println("\n");
					availableCommands();
					break;
				case 4:
					Management.printWholeBase();
					System.out.println("\n");
					availableCommands();
					break;
				case 0:
					availableCommands();					
				default:
					System.out.println("There's no such option. Please, try again.");
					printRecordsDialog();
					break;
			}
		} catch (NumberFormatException e) {
			System.out.println("Sorry, can't process your choise. Try again.");
			printRecordsDialog();
		}
	}
	
	static void createObjectsDialog() {
		System.out.println("What object you'd like to create?\n");
		System.out.println("1. Client\n2. Car\n3. Create new order to rent a car\n4. Create new manager\n0. Go back to main menu");
		
		String command = new Scanner(System.in).nextLine();
		
		try {
			switch (Integer.valueOf(command)) {
			case 1:
				System.out.println("Name: ");
				String name = new Scanner(System.in).nextLine();
				System.out.println("Passport ID: ");
				String pid = new Scanner(System.in).nextLine();
				System.out.println("Amount of money: ");
				String money = new Scanner(System.in).nextLine();
				System.out.println("Password: ");
				String pwd = new Scanner(System.in).nextLine();
				System.out.println("Email: ");
				String email = new Scanner(System.in).nextLine();
				if (Management.create(name, pid, Float.valueOf(money), pwd, email) != null) {
					System.out.println("New client is created!\n");
				}
				createObjectsDialog();
				break;
			case 2:
				System.out.println("Model: ");
				String model = new Scanner(System.in).nextLine();
				System.out.println("Price: ");
				String price = new Scanner(System.in).nextLine();
				System.out.println("Type: ");
				String type = new Scanner(System.in).nextLine();
				if (Management.create(model, Float.valueOf(price), type) != null) {
					System.out.println("New car is created!\n");
				}
				createObjectsDialog();
				break;
			case 3:
				System.out.println("Enter manager's name: ");
				String mn = new Scanner(System.in).nextLine();
				System.out.println("Client ID: ");
				String cId = new Scanner(System.in).nextLine();
				System.out.println("Car id: ");
				String carId = new Scanner(System.in).nextLine();
				System.out.println("Enter today's date (YYYY-MM-DD):");
				String creationDate = new Scanner(System.in).nextLine();
				try {
					Management.getCarById(Long.valueOf(carId));
					Management.getManagerByName(mn);
				} catch (NumberFormatException e) {
					try {
						System.out.println("The ID you've entered is invalid! (perhaps, contains letters)\n");
						throw new NoSuchCarException("The ID you've entered is invalid!  (perhaps, contains letters)\n", e);
					} catch (NoSuchCarException e1) {
						createObjectsDialog();
					}
				} catch (IllegalArgumentException e) {
					try {
						System.out.println("The ID you've entered is invalid! \n");
						throw new NoSuchCarException("The ID you've entered is invalid! \n");
					} catch (NoSuchCarException e1) {
						createObjectsDialog();
					}
//				}catch(NoSuchManagerException e){
//					e.printStackTrace();
				}
//				try {
					if(Management.rentCar(Management.getManagerByName(mn), Management.getCarById(Long.valueOf(carId)),
							Management.getClientByPID(cId), creationDate) != null){
						System.out.println("New order is created!\n");	
					}
//				}
//				} catch (NoSuchManagerException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				createObjectsDialog();
				break;
			case 4:
				System.out.println("Name: ");
				String manName = new Scanner(System.in).nextLine();
				System.out.println("Password: ");
				String manPwd = new Scanner(System.in).nextLine();
				if (Management.create(manName, manPwd) != null) {
					System.out.println("New manager is created!\n");
				}
				createObjectsDialog();
				break;
			case 0:
				availableCommands();
			default:
				System.out.println("There's no such option. Please, try again.");
				createObjectsDialog();
				break;
			}
		} catch (NumberFormatException e) {
			System.out.println("Sorry, can't process your choise. Try again.");
			createObjectsDialog();
		}
	}
}